home *** CD-ROM | disk | FTP | other *** search
- Path: faatcrl.faa.gov!saratoga!lbona
- From: lbona@saratoga (lbona)
- Newsgroups: comp.lang.c
- Subject: returning ptr to struct with ptrs in it?
- Date: 23 Feb 1996 11:24:18 GMT
- Organization: FAA Technical Center, Pomona, NJ
- Message-ID: <4gk852INNbp4@faatcrl.faa.gov>
- NNTP-Posting-Host: 155.178.247.116
- X-Newsreader: Tin 1.1 PL4
-
-
- The following is beyond the scope of my knowledge. Perhaps someone can explain
- it to me:
-
- When I declare a function that returns a pointer to a struct that contains
- pointers, the pointer(s) at the end of the struct get set to garbage after
- being returned. Pointers not at the end are not affected.
-
- --------------------------------------------------------------
-
- typedef struct aaa {
- int i;
- char name[22];
- int id;
- } AAA;
-
- typedef struct bar {
- long a;
- long b;
- int c;
- AAA *d;
- char e;
- char f[100];
- AAA *g;
- AAA *h;
- } BAR;
-
- BAR *parse_bar(char toke[]);
-
- main()
- {
- BAR BB;
- char toke[128];
-
- memset(&BB,0,sizeof(BAR));
- /* at this point BB.d == BB.g == BB.h == NULL */
-
- /* toke is an ascii string read in from a file */
- BB = *parse_bar(toke);
-
- /* at this point BB.g and BB.h are garbage, but all the other fields */
- /* in BB are correct including BB.d*/
-
- }
-
-
- BAR *parse_bar(char toke[]);
- {
- BAR bb;
-
- memset(&bb,0,sizeof(BAR));
- /* at this point BB.d == BB.g == BB.h == NULL */
-
- /* read data from file and parse - never touch bb.d, bb.g, or bb.h */
-
- /* at this point BB.d == BB.g == BB.h == NULL */
- return(&bb);
- }
-
-
- ----------------------------------------------------------
-
- I know there are several work arounds, and that it's better to pass the address
- of BB to parse_bar(), which is what I'll be doing. But I'm curious why this is
- happening. I've tried it under K&R on Unix and ANSI on DOS and the results
- were identical.
-
-
- Any help would be appreciated,
-
- thanks
-
- --
-
- Lou Bona
- lbona@tgf.tc.faa.gov
-
-